﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-15 10:51:03
 * 功能：记录容器（一个容器中最多放3个关卡记录）
**************************************************************************************/

using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class RecordContainer : MonoBehaviour
    {
        private HorizontalLayoutGroup m_Layout;
        private RecordItem[] m_Items;

        private void Find()
        {
            m_Layout = GetComponent<HorizontalLayoutGroup>();
            m_Items = GetComponentsInChildren<RecordItem>(true);
        }

        public void Init(int minLv, int maxLv, HallPanelView rootView)
        {
            if (m_Layout == null) Find();
            int count = maxLv - minLv + 1; // 总共需要显示的关卡数
            m_Layout.spacing = count >= m_Items.Length ? 0 : -400;
            m_Layout.reverseArrangement = count < m_Items.Length;

            for (int i = 0; i < m_Items.Length; i++)
                m_Items[i].gameObject.SetActive(i < count);

            for (int lv = minLv; lv <= maxLv; lv++)
            {
                var lvInfo = GameDataMgr.Instance.GetLevel(lv);
                m_Items[lv - minLv].Init(lvInfo, lv, rootView);
            }
        }

        public void Clear()
        {
            for (int i = 0; i < m_Items.Length; i++)
            {
                m_Items[i].Clear();
                m_Items[i].gameObject.SetActive(false);
            }
        }
    }
}